package com.bw.lib_storage.mmkv

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class MMKVProperty<T>(private val deValue: T):ReadWriteProperty<MMKVOwner,T> {
    override fun getValue(thisRef: MMKVOwner, property: KProperty<*>): T {
       /* when(deValue){
            is Int -> return thisRef.mmkv.getInt(property.name,deValue) as T
            is String ->return thisRef.mmkv.getString(property.name,deValue) as T
            is Boolean -> return thisRef.mmkv.getBoolean(property.name,deValue) as T
            is Float -> return thisRef.mmkv.getFloat(property.name,deValue) as T
            is Long -> return thisRef.mmkv.getLong(property.name,deValue) as T
            else -> return Unit as T
        }*/
        return thisRef.mmkv.run {
            when(deValue){
                is Int -> getInt(property.name,deValue)
                is String ->getString(property.name,deValue)
                is Boolean -> getBoolean(property.name,deValue)
                is Float -> getFloat(property.name,deValue)
                is Long -> getLong(property.name,deValue)
                else -> Unit
            } as T
        }

    }

    override fun setValue(thisRef: MMKVOwner, property: KProperty<*>, value: T) {
        thisRef.mmkv.run {
            when(value){
                is Int -> putInt(property.name,value)
                is String ->putString(property.name,value)
                is Boolean -> putBoolean(property.name,value)
                is Float -> putFloat(property.name,value)
                is Long -> putLong(property.name,value)
                else -> Unit
            }
        }
    }
}